﻿using Data;
using PayrollExpress.Helpers;
using PayrollExpress.Models;
using System;
using System.Web.Mvc;

namespace PayrollExpress.Controllers
{
    public class BaseController: Controller
    {
        const string error = "You are not authorized to view this page.";

        public void CheckUserPageAccess(BaseHelper.Page page)
        { 
            using (var db = new DatabaseContext())
            {
                var access = BaseHelper.GetUserAccess(db, User.Identity.Name);
                var role = BaseHelper.GetUserRoleID(db, User.Identity.Name);

                switch (page)
                {
                    #region Administration access
                    case BaseHelper.Page.Users:
                        {
                            if (!access.HasUsersAccess || role != (int)BaseHelper.UserRole.Admin)
                            { throw new Exception(error); }
                            break;
                        }

                    case BaseHelper.Page.EmploymentTypes:
                        {
                            if (!access.HasEmploymentTypesAccess || role != (int)BaseHelper.UserRole.Admin)
                            { throw new Exception(error); }
                            break;
                        }

                    //case BaseHelper.Page.HolidayTypes:
                    //    {
                    //        if (!access.HasHolidayTypesAccess || role != (int)BaseHelper.UserRole.Admin)
                    //        { throw new Exception(error); }
                    //        break;
                    //    }

                    case BaseHelper.Page.WorkLocations:
                        {
                            if (!access.HasWorkLocationsAccess || role != (int)BaseHelper.UserRole.Admin)
                            { throw new Exception(error); }
                            break;
                        }

                    #endregion

                    #region Masterfile access

                    case BaseHelper.Page.Holidays:
                        {
                            if (!access.HasHolidaysAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case BaseHelper.Page.Employees:
                        {
                            if (!access.HasEmployeesAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case BaseHelper.Page.EmployeeContracts:
                        {
                            if (!access.HasEmployeeContractsAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    #endregion

                    #region Workspace

                    case BaseHelper.Page.Payroll:
                        {
                            if (!access.HasPayrollAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    #endregion

                    #region Reports access

                    case BaseHelper.Page.Reports:
                        {
                            if (!access.HasReportsAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    #endregion
                }
            }
            
        }

        public ViewResult Error(Exception ex)
        {
            return View("_Error", ex);
        }
    }
}